<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="货位编码:">
        <el-input v-model="queryParams.lotCd" size="small" placeholder="请输入货位编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="拣货优先度:">
        <el-input v-model="queryParams.pickUpIndex" size="small" placeholder="请输入拣货优先度" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="最大载重:">
        <el-input v-model="queryParams.maxCapacity" size="small" placeholder="请输入最大载重" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="最大容积:">
        <el-input v-model="queryParams.maxVol" size="small" placeholder="请输入最大容积" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="层高:">
        <el-input v-model="queryParams.storage" size="small" placeholder="请输入层高" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="仓库编码:">
        <el-input v-model="queryParams.whCd" size="small" placeholder="请输入仓库编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="域:">
        <el-input v-model="queryParams.site" size="small" placeholder="请输入域" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label=":">
        <el-input v-model="queryParams.version" size="small" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="货区编码:">
        <el-input v-model="queryParams.locationNo" size="small" placeholder="请输入货区编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['web:tbWmsLot:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['web:tbWmsLot:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['web:tbWmsLot:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['web:tbWmsLot:export']" type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="货位编码" align="center" prop="lotCd" />
      <el-table-column label="拣货优先度" align="center" prop="pickUpIndex" />
      <el-table-column label="最大载重" align="center" prop="maxCapacity" />
      <el-table-column label="最大容积" align="center" prop="maxVol" />
      <el-table-column label="层高" align="center" prop="storage" />
      <el-table-column label="仓库编码" align="center" prop="whCd" />
      <el-table-column label="域" align="center" prop="site" />
      <el-table-column label="" align="center" prop="version" />
      <el-table-column label="货区编码" align="center" prop="locationNo" />
      <el-table-column label="" align="center" prop="id" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['web:tbWmsLot:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['web:tbWmsLot:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="tbWmsLotFormDialog.title" :visible.sync="tbWmsLotFormDialog.visible" :close-on-click-modal="false" width="500px" append-to-body>
      <el-form ref="tbWmsLotForm" :model="tbWmsLotFormData" :rules="tbWmsLotFormRules" label-width="80px">
        <el-form-item label="货位编码" prop="lotCd">
          <el-input v-model="tbWmsLotFormData.lotCd" placeholder="请输入货位编码" />
        </el-form-item>
        <el-form-item label="拣货优先度" prop="pickUpIndex">
          <el-input v-model="tbWmsLotFormData.pickUpIndex" placeholder="请输入拣货优先度" />
        </el-form-item>
        <el-form-item label="最大载重" prop="maxCapacity">
          <el-input v-model="tbWmsLotFormData.maxCapacity" placeholder="请输入最大载重" />
        </el-form-item>
        <el-form-item label="最大容积" prop="maxVol">
          <el-input v-model="tbWmsLotFormData.maxVol" placeholder="请输入最大容积" />
        </el-form-item>
        <el-form-item label="层高" prop="storage">
          <el-input v-model="tbWmsLotFormData.storage" placeholder="请输入层高" />
        </el-form-item>
        <el-form-item label="仓库编码" prop="whCd">
          <el-input v-model="tbWmsLotFormData.whCd" placeholder="请输入仓库编码" />
        </el-form-item>
        <el-form-item label="域" prop="site">
          <el-input v-model="tbWmsLotFormData.site" placeholder="请输入域" />
        </el-form-item>
        <el-form-item label="" prop="version">
          <el-input v-model="tbWmsLotFormData.version" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="货区编码" prop="locationNo">
          <el-input v-model="tbWmsLotFormData.locationNo" placeholder="请输入货区编码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmTbWmsLotFormDialog">确 定</el-button>
        <el-button @click="resetTbWmsLotFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import TbWmsLotApi from '@/api/web/TbWmsLotApi'

const defaultTbWmsLotFormData = {
  lotCd: null,
  pickUpIndex: null,
  maxCapacity: null,
  maxVol: null,
  storage: null,
  whCd: null,
  site: null,
  version: null,
  locationNo: null,
}
export default {
  name: 'TbWmsLotList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        lotCd: null,
        pickUpIndex: null,
        maxCapacity: null,
        maxVol: null,
        storage: null,
        whCd: null,
        site: null,
        version: null,
        locationNo: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      tbWmsLotFormDialog: {
        title: '',
        visible: false
      },
      tbWmsLotFormData: Object.assign({}, defaultTbWmsLotFormData),
      tbWmsLotFormRules: {
        lotCd: [{ required: true, message: '货位编码不能为空', trigger: 'blur' }],
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openTbWmsLotFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return TbWmsLotApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openTbWmsLotFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      TbWmsLotApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return TbWmsLotApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    openTbWmsLotFormDialog(id) {
      this.resetTbWmsLotFormDialog()
      this.tbWmsLotFormDialog.visible = true
      if (id) {
        this.tbWmsLotFormDialog.title = '货位-修改'
        TbWmsLotApi.get(id).then(response => {
          this.tbWmsLotFormData = {
            ...this.tbWmsLotFormData,
            ...response.data
          }
        })
      } else {
        this.tbWmsLotFormDialog.title = '货位-添加'
      }
    },
    confirmTbWmsLotFormDialog() {
      this.$refs['tbWmsLotForm'].validate(valid => {
        if (valid) {
          if (this.tbWmsLotFormData.id) {
            TbWmsLotApi.edit(this.tbWmsLotFormData).then(response => {
              this.$message.success('修改成功')
              this.tbWmsLotFormDialog.visible = false
              this.handleQuery()
            })
          } else {
            TbWmsLotApi.add(this.tbWmsLotFormData).then(response => {
              this.$message.success('添加成功')
              this.tbWmsLotFormDialog.visible = false
              this.handleQuery()
            })
          }
        }
      })
    },
    resetTbWmsLotFormDialog() {
      this.tbWmsLotFormDialog = {
        title: '',
        visible: false
      }
      this.tbWmsLotFormData = Object.assign({}, defaultTbWmsLotFormData)
      this.resetForm('tbWmsLotForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
